package cn.schoolwow.quickdao.module.database.dql.response.flow.common.resultSet;

import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class MatchEntityFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        Entity entity = flowContext.getData("entity", Entity.class);

        if(null==entity){
            flowContext.log("实体类不存在!跳过根据实体类设置字段类型");
            return;
        }

        Property[] resultSetPropertyArray = (Property[]) flowContext.checkData("resultSetPropertyArray");
        for(Property property:entity.properties){
            for(Property resultSetProperty:resultSetPropertyArray){
                if(!resultSetProperty.entity.tableName.equalsIgnoreCase(entity.tableName)){
                    continue;
                }
                if(resultSetProperty.columnLabel.equalsIgnoreCase(property.name)||resultSetProperty.columnLabel.equalsIgnoreCase(property.column)){
                    resultSetProperty.clazz = property.clazz;
                    break;
                }
            }
        }
    }

    @Override
    public String name() {
        return "匹配实体类字段类型";
    }
}
